home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------
-
- arrowpair.c
-
- This reusable module implements "arrow pair" controls. These controls
- can be used for go backwards / go forwards operations. The arrows
- can be an up/down pair or a left/right pair.
-
- This could also be done with a cdef, of course.
-
- Copyright © 1994-1995, Northwestern University.
-
- ----------------------------------------------------------------------------*/
-
- #include "arrowpair.h"
- #include "memutil.h"
- #include "drawutil.h"
-
-
-
- typedef struct TPairInfo {
- Boolean upDown; /* true if up/down, false if left/right */
- Rect arrowRect; /* rectangle enclosing the arrows */
- Rect backwardHotRect; /* hot rect for backward arrow */
- Rect forwardHotRect; /* hot rect for forward arrow */
- PolyHandle backwardArrow; /* handle to backward arrow polygon */
- PolyHandle forwardArrow; /* handle to forward arrow polygon */
- Boolean backwardEnabled; /* true if backward arrow enabled */
- Boolean forwardEnabled; /* true if forward arrow enabled */
- } TPairInfo;
-
-
-
- static RGBColor gLightBlue = {0x9999, 0x9999, 0xFFFF}; /* light blue for filling arrows */
-
-
-
- /*----------------------------------------------------------------------------
- CreateArrowPair
-
- Create an arrow pair.
-
- Entry: upDown = true if up/down arrows, false if left/right.
- arrowRect = pointer to rectangle enclosing the arrows.
- backwardHotRect = pointer to hot rectangle for backward
- arrow (up/left).
- forwardHotRect = pointer to hot rectangle for forward
- arrow (down/right).
-
- Exit: function result = error code.
- *arrowPair = new arrow pair reference.
- ----------------------------------------------------------------------------*/
-
- OSErr CreateArrowPair (Boolean upDown, Rect *arrowRect, Rect *backwardHotRect,
- Rect *forwardHotRect, ArrowPairRef *arrowPair)
- {
- OSErr err = noErr;
- TPairInfo **t;
- PolyHandle p;
- short height, indent;
-
- err = MyNewHandle(sizeof(TPairInfo), &t);
- if (err != noErr) return err;
- (**t).upDown = upDown;
- (**t).arrowRect = *arrowRect;
- (**t).backwardHotRect = *backwardHotRect;
- (**t).forwardHotRect = *forwardHotRect;
- (**t).backwardEnabled = true;
- (**t).forwardEnabled = true;
-
- if (upDown) {
- height = (arrowRect->right - arrowRect->left) >> 1;
- } else {
- height = (arrowRect->bottom - arrowRect->top) >> 1;
- }
- indent = height >> 1;
-
- p = OpenPoly();
- if (upDown) {
- MoveTo(arrowRect->left, arrowRect->top + height);
- LineTo(arrowRect->left + indent, arrowRect->top + height);
- LineTo(arrowRect->left + indent, arrowRect->top + height + indent);
- LineTo(arrowRect->right - indent, arrowRect->top + height + indent);
- LineTo(arrowRect->right - indent, arrowRect->top + height);
- LineTo(arrowRect->right, arrowRect->top + height);
- LineTo(arrowRect->left + height, arrowRect->top);
- LineTo(arrowRect->left, arrowRect->top + height);
- } else {
- MoveTo(arrowRect->left + height, arrowRect->bottom);
- LineTo(arrowRect->left + height, arrowRect->bottom - indent);
- LineTo(arrowRect->left + height + indent, arrowRect->bottom - indent);
- LineTo(arrowRect->left + height + indent, arrowRect->top + indent);
- LineTo(arrowRect->left + height, arrowRect->top + indent);
- LineTo(arrowRect->left + height, arrowRect->top);
- LineTo(arrowRect->left, arrowRect->top + height);
- LineTo(arrowRect->left + height, arrowRect->bottom);
- }
- ClosePoly();
- (**t).backwardArrow = p;
-
- p = OpenPoly();
- if (upDown) {
- MoveTo(arrowRect->left, arrowRect->bottom - height);
- LineTo(arrowRect->left + indent, arrowRect->bottom - height);
- LineTo(arrowRect->left + indent, arrowRect->bottom - height - indent);
- LineTo(arrowRect->right - indent, arrowRect->bottom - height - indent);
- LineTo(arrowRect->right - indent, arrowRect->bottom - height);
- LineTo(arrowRect->right, arrowRect->bottom - height);
- LineTo(arrowRect->left + height, arrowRect->bottom);
- LineTo(arrowRect->left, arrowRect->bottom - height);
- } else {
- MoveTo(arrowRect->right - height, arrowRect->bottom);
- LineTo(arrowRect->right - height, arrowRect->bottom - indent);
- LineTo(arrowRect->right - height - indent, arrowRect->bottom - indent);
- LineTo(arrowRect->right - height - indent, arrowRect->top + indent);
- LineTo(arrowRect->right - height, arrowRect->top + indent);
- LineTo(arrowRect->right - height, arrowRect->top);
- LineTo(arrowRect->right, arrowRect->top + height);
- LineTo(arrowRect->right - height, arrowRect->bottom);
- }
- ClosePoly();
- (**t).forwardArrow = p;
-
- *arrowPair = (ArrowPairRef)t;
- return noErr;
- }
-
-
-
- /*----------------------------------------------------------------------------
- DisposeArrowPair
-
- Dispose an arrow pair.
-
- Entry: arrowPair = arrow pair reference.
- ----------------------------------------------------------------------------*/
-
- void DisposeArrowPair (ArrowPairRef arrowPair)
- {
- TPairInfo **t;
-
- if (arrowPair == nil) return;
- t = (TPairInfo**)arrowPair;
- if ((**t).backwardArrow != nil) KillPoly((**t).backwardArrow);
- if ((**t).forwardArrow != nil) KillPoly((**t).forwardArrow);
- MyDisposeHandle(t);
- }
-
-
-
- /*----------------------------------------------------------------------------
- DrawArrowPair
-
- Draw an arrow pair.
-
- Entry: arrowPair = arrow pair reference.
- ----------------------------------------------------------------------------*/
-
- void DrawArrowPair (ArrowPairRef arrowPair)
- {
- TPairInfo **t;
- PolyHandle backwardArrow, forwardArrow;
-
- t = (TPairInfo**)arrowPair;
- backwardArrow = (**t).backwardArrow;
- forwardArrow = (**t).forwardArrow;
- if ((**t).backwardEnabled) {
- FillColorPoly(backwardArrow, &gLightBlue, &qd.gray);
- FramePoly(backwardArrow);
- } else {
- FrameGrayPoly(backwardArrow);
- }
- if ((**t).forwardEnabled) {
- FillColorPoly(forwardArrow, &gLightBlue, &qd.gray);
- FramePoly(forwardArrow);
- } else {
- FrameGrayPoly(forwardArrow);
- }
- }
-
-
-
- /*----------------------------------------------------------------------------
- TrackArrowPair
-
- Track an arrow pair.
-
- Entry: arrowPair = arrow pair reference.
-
- Exit: function result =
- 0 if no hit.
- +1 if hit in forward arrow (down/right)
- -1 if hit in backward arrow (up/left)
- ----------------------------------------------------------------------------*/
-
- short TrackArrowPair (ArrowPairRef arrowPair)
- {
- TPairInfo **t;
- Boolean filled;
- Point where;
- Rect hot;
- PolyHandle p;
- short result;
-
- t = (TPairInfo**)arrowPair;
-
- GetMouse(&where);
- if (PtInRect(where, &(**t).backwardHotRect)) {
- if (!(**t).backwardEnabled) return 0;
- hot = (**t).backwardHotRect;
- p = (**t).backwardArrow;
- result = -1;
- } else if (PtInRect(where, &(**t).forwardHotRect)) {
- if (!(**t).forwardEnabled) return 0;
- hot = (**t).forwardHotRect;
- p = (**t).forwardArrow;
- result = +1;
- } else {
- return 0;
- }
- FillPoly(p, &qd.black);
- filled = true;
- while (StillDown()) {
- GetMouse(&where);
- if (PtInRect(where, &hot)) {
- if (!filled) {
- FillPoly(p, &qd.black);
- filled = true;
- }
- } else {
- if (filled) {
- ErasePoly(p);
- FillColorPoly(p, &gLightBlue, &qd.gray);
- FramePoly(p);
- filled = false;
- }
- }
- }
- if (filled) {
- ErasePoly(p);
- FillColorPoly(p, &gLightBlue, &qd.gray);
- FramePoly(p);
- return result;
- }
- return 0;
- }
-
-
-
- /*----------------------------------------------------------------------------
- MoveArrowPair
-
- Move an arrow pair.
-
- Entry: arrowPair = arrow pair reference.
- botLeft = new coordinates of bottom/left corner of rectangle
- enclosing the arrows.
- ----------------------------------------------------------------------------*/
-
- void MoveArrowPair (ArrowPairRef arrowPair, Point botLeft)
- {
- TPairInfo **t;
- short dh, dv;
-
- t = (TPairInfo**)arrowPair;
- dh = botLeft.h - (**t).arrowRect.left;
- dv = botLeft.v - (**t).arrowRect.bottom;
- OffsetRect(&(**t).arrowRect, dh, dv);
- OffsetRect(&(**t).backwardHotRect, dh, dv);
- OffsetRect(&(**t).forwardHotRect, dh, dv);
- OffsetPoly((**t).backwardArrow, dh, dv);
- OffsetPoly((**t).forwardArrow, dh, dv);
- }
-
-
-
- /*----------------------------------------------------------------------------
- TestArrowPairHotRect
-
- Test to see if a location is inside an arrow pair hot rect.
-
- Entry: arrowPair = arrow pair reference.
- where = location in local coordinates.
-
- Exit: function result =
- -1 if location is in backward arrow hot rect.
- 0 if location is not in a hot rect.
- +1 if location is in forward arrow hot rect.
- *hotRect = hot rect if function result != 0.
- *enabled = true if arrow is enabled if result != 0.
- ----------------------------------------------------------------------------*/
-
- short TestArrowPairHotRect (ArrowPairRef arrowPair, Point where, Rect *hotRect,
- Boolean *enabled)
- {
- TPairInfo **t;
-
- if (arrowPair == nil) return 0;
- t = (TPairInfo**)arrowPair;
- if (PtInRect(where, &(**t).backwardHotRect)) {
- *hotRect = (**t).backwardHotRect;
- *enabled = (**t).backwardEnabled;
- return - 1;
- } else if (PtInRect(where, &(**t).forwardHotRect)) {
- *hotRect = (**t).forwardHotRect;
- *enabled = (**t).forwardEnabled;
- return +1;
- } else {
- return 0;
- }
- }
-
-
-
- /*----------------------------------------------------------------------------
- FlashArrowPair
-
- Flash an arrow pair.
-
- Entry: arrowPair = arrow pair reference.
- whichArrow =
- -1 to flash backward arrow (left/top).
- +1 to flash forward arrow (right/bottom).
-
- Exit: *hotRect = hot rect.
- ----------------------------------------------------------------------------*/
-
- void FlashArrowPair (ArrowPairRef arrowPair, short whichArrow)
- {
- TPairInfo **t;
- PolyHandle p;
- long myTicks;
-
- t = (TPairInfo**)arrowPair;
- if (whichArrow == -1) {
- if (!(**t).backwardEnabled) return;
- p = (**t).backwardArrow;
- } else {
- if (!(**t).forwardEnabled) return;
- p = (**t).forwardArrow;
- }
- FillPoly(p, &qd.black);
- Delay(8, &myTicks);
- ErasePoly(p);
- FillColorPoly(p, &gLightBlue, &qd.gray);
- FramePoly(p);
- }
-
-
-
- /*----------------------------------------------------------------------------
- EnableOrDisableArrowPair
-
- Enable or disable an arrow pair.
-
- Entry: arrowPair = arrow pair reference.
- whichArrow =
- -1 to enable/disable backward arrow (left/top).
- +1 to enable/disable forward arrow (right/bottom).
- enable = true to enable arrow, false to disable it.
- ----------------------------------------------------------------------------*/
-
- void EnableOrDisableArrowPair (ArrowPairRef arrowPair, short whichArrow, Boolean enable)
- {
- TPairInfo **t;
- PolyHandle p;
-
- t = (TPairInfo**)arrowPair;
- if (whichArrow == -1) {
- if ((**t).backwardEnabled == enable) return;
- (**t).backwardEnabled = enable;
- p = (**t).backwardArrow;
- } else {
- if ((**t).forwardEnabled == enable) return;
- (**t).forwardEnabled = enable;
- p = (**t).forwardArrow;
- }
- ErasePoly(p);
- if (enable) {
- FillColorPoly(p, &gLightBlue, &qd.gray);
- FramePoly(p);
- } else {
- FrameGrayPoly(p);
- }
- }
-